#include "eutranMsgHandle.h"
#include "common.h"
#include "eutranManager.h"
#include "EutranConfigData.h"
#include "EutranTcpReceiver.h"
#include "../logServer/logger.h"
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>

#ifdef MODULE_TEST
static bool testUdp = false;
#endif

static UE_REDIRECT redirect_config_info[7];

UE_REDIRECT *get_redirect_info(void)
{
    return redirect_config_info;
}

void split(std::string& s, const std::string& delim, std::vector<std::string> *ret)
{
    unsigned last = 0;
    unsigned index = s.find_first_of(delim, last);
    while(index != std::string::npos)
    {
        ret->push_back(s.substr(last,index-last));
        last = index+1;
        index = s.find_first_of(delim,last);
    }

    if(index-last > 0)
    {
        ret->push_back(s.substr(last,index-last));
    }
}

bool convertToEarfcnList(const char* paraValue, uint32_t paraLength, std::vector<std::pair<uint16_t, uint16_t> >& arfcnRangeList)
{
    if( paraValue==NULL ||paraLength==0 )
    {
        EMLogError("paraValue is NULL[%d] or length is 0[%d]", paraValue==NULL, paraLength==0);
        return false;
    }
    uint32_t startIndex = 0;
    uint32_t endIndex = 0;
    uint16_t lowerArfcn = 0, upperArfcn = 0;
    bool isLowerArfcnPresent;
    for(uint32_t i=0; ;i++, endIndex = i)
    {
        if( i==paraLength )
        {
            if( endIndex==startIndex )
            {
                continue;
            }
            std::string s(&paraValue[startIndex], endIndex-startIndex);
            upperArfcn = atoi(s.c_str());
            startIndex = i+1;
            if( lowerArfcn>upperArfcn || !isLowerArfcnPresent)
            {
                EMLogError("invalid parameter: lowerArfcn=[%d], upperArfcn=[%d]", lowerArfcn, upperArfcn);
                return false;
            }
            arfcnRangeList.push_back(std::make_pair<uint16_t, uint16_t>(lowerArfcn, upperArfcn));
            break;
        }
        else if( paraValue[i]=='-' )
        {
            if( endIndex==startIndex )
            {
                continue;
            }
            std::string s(&paraValue[startIndex], endIndex-startIndex);
            lowerArfcn = atoi(s.c_str());
            startIndex = i+1;
            isLowerArfcnPresent = true;
        }
        else if( paraValue[i]==',' )
        {
            if( endIndex==startIndex )
            {
                continue;
            }
            std::string s(&paraValue[startIndex], endIndex-startIndex);
            upperArfcn = atoi(s.c_str());
            startIndex = i+1;
            if( lowerArfcn>upperArfcn || !isLowerArfcnPresent)
            {
                EMLogError("Invalid parameter: lowerArfcn=[%d], upperArfcn=[%d]\n", lowerArfcn, upperArfcn);
                return false;
            }
            arfcnRangeList.push_back(std::make_pair<uint16_t, uint16_t>(lowerArfcn, upperArfcn));
            isLowerArfcnPresent = false;
        }
        else if( paraValue[i]>'9' || paraValue[i]<'0' )
        {
            EMLogError("Invalid parameter[%d]: %c", i, paraValue[i]);
            return false;
        }
        
    }
    return true;
}

bool convertToPciList(const char* paraValue, uint32_t paraLength, std::vector<uint16_t>& pciList)
{
    if( paraValue==NULL ||paraLength==0 )
    {
        EMLogError("paraValue is NULL[%d] or length is 0[%d]", paraValue==NULL, paraLength==0);
        return false;
    }
    uint32_t startIndex = 0;
    uint32_t endIndex = 0;
    for(uint32_t i=0; ;i++, endIndex = i)
    {
        if( i==paraLength )
        {
            if( endIndex==startIndex )
            {
                break;
            }
            std::string s(&paraValue[startIndex], endIndex-startIndex);
            uint16_t pci = atoi(s.c_str());
            if( pci>503 )
            {
                EMLogError("pci[%d] is out of range", pci);
                return false;
            }
            pciList.push_back(pci);
            break;
        }
        else if( paraValue[i]==',' )
        {
            if( endIndex==startIndex )
            {
                continue;
            }
            std::string s(&paraValue[startIndex], endIndex-startIndex);
            uint16_t pci = atoi(s.c_str());
            if( pci>503 )
            {
                EMLogError("pci[%d] is out of range", pci);
                return false;
            }
            pciList.push_back(pci);
            
            startIndex = i+1;
        }
        else if( paraValue[i]>'9' || paraValue[i]<'0' )
        {
            EMLogError("Invalid parameter[%d]: %c", i, paraValue[i]);
            return false;
        }
        
    }
    return true;
}

static void* eutranUdpRecv(void *arg __attribute__((unused)))
{
    dbgprintf(DEBUG_INFO, stdout, "eutranUdpReceiver thread id is %lu \n", pthread_self()); 
    pthread_setname_np(pthread_self(), "eutranUdpRecv");
    
    struct sockaddr_in localAddr;
    localAddr.sin_family = AF_INET;
    char* envString = getenv("x86Ipaddr");
    if( envString!=NULL )
    {
        localAddr.sin_addr.s_addr = inet_addr(envString);
    }
    else
    {
        localAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    }
    envString = getenv("x86port");
    if( envString!=NULL )
    {
        localAddr.sin_port = htons(atoi(envString));
    }
    else
    {
        localAddr.sin_port = htons(32790);
    }

    dbgprintf(DEBUG_INFO, stdout, "ip is 0x%x; port is %d\n", localAddr.sin_addr.s_addr, ntohs(localAddr.sin_port));
    
    int fd = socket(AF_INET, SOCK_DGRAM, 0);
    if( fd==-1 )
    {
        EMLogError("Fail to create socket, errno[%d]", errno);
        return NULL;
    }

    if( bind(fd, (struct sockaddr *)&localAddr, sizeof(localAddr))==-1 )
    {
        EMLogError("Fail to bind socket, errno[%d]", errno);
        return NULL;
    }

    if (-1 == fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)& (~O_NONBLOCK)))
    {
        EMLogError("fcntl sock errot, errno[%d]", errno);
        return NULL;        
    }

    struct timeval tv;
    fd_set readfds;
    tv.tv_sec = 1;
    tv.tv_usec = 0;

    uint8_t recvMsgBuf[65535] = {0};
    uint8_t internalMsgBuffer[65535] = {0};

    while(1)
    {
        struct sockaddr_in peerAddr;
        socklen_t len = sizeof(peerAddr);
        memset(&peerAddr, 0, len);
        EutranMgrPtr->setThreadStat(true);

//here for test
#ifdef MODULE_TEST
        if (testUdp )
        {
            EMLogError("---------- test module eutran udp rcv thread ---------------- %04x\n");
            while(1)
            {

            }
        }
#endif
        FD_ZERO(&readfds);
        FD_SET(fd, &readfds);
        tv.tv_sec = 1;
        tv.tv_usec = 0;
        if (select(fd+1,&readfds,NULL, NULL, &tv) <= 0)
            continue;

        int recv_len = -1;
        do
        {
            recv_len = recvfrom(fd, recvMsgBuf, sizeof(recvMsgBuf), 0, (struct sockaddr *)&peerAddr, &len);
        }
        while ( (recv_len == -1) && (errno == EINTR) );

        MSGHEADER msgHdr;
        memset(&msgHdr, 0, sizeof(msgHdr));
        msgHdr.dstID = MODULE_EUTRAN_SNIFFER;
        msgHdr.srcID = MODULE_EUTRAN_SNIFFER;
        msgHdr.msgType = MSG_TYPE_INTERNAL;

        memcpy(internalMsgBuffer, &(peerAddr.sin_addr.s_addr), sizeof(in_addr_t));
        memcpy(internalMsgBuffer+sizeof(in_addr_t), recvMsgBuf, recv_len);
        msgHdr.msgLength = recv_len+sizeof(in_addr_t)+sizeof(MSGHEADER);

        if( RC_OK!=MSG_send(MODULE_EUTRAN_SNIFFER, &msgHdr, internalMsgBuffer) )
        {
            EMLogError("Fail to send msg");
        }
    }
    return NULL;
}

static void* eutranMainTask(void *arg __attribute__((unused)))
{
    dbgprintf(DEBUG_INFO, stderr, "eutranMainTask id is %lu \n", pthread_self()); 
    pthread_setname_np(pthread_self(), "eutranMainTask");
    
    uint8_t recvMsgBuf[65535] = {0};
    MSGHEADER *msgHeader = (MSGHEADER *)recvMsgBuf;
    void* payload = (void*)(recvMsgBuf + sizeof(MSGHEADER));

    bool initRc = false;
    
    while( 1 )
    {
        if( !initRc )
        {
            initRc = EutranTcpReceiverPtr->initialize();

            if(!initRc )
            {
                EMLogError("Fail to init eutranTcpReceiver");
            }
        }
        if( RC_OK == MSG_read_nonblock(MODULE_EUTRAN_SNIFFER, msgHeader, payload) )
        {
            switch(msgHeader->msgType)
            {
                case MSG_TYPE_TICK:
                {
                    EutranMgrPtr->handleTimerTick();
                    break;
                }
                case MSG_TYPE_INTERNAL: 
                {
                    in_addr_t* addr = (in_addr_t*)payload;
                    uint32_t sizeOfAddr = sizeof(in_addr_t);
                    uint8_t* ptr = (uint8_t*)payload;
                    ptr += sizeOfAddr;
                    EutranMgrPtr->processBbuMessage(ptr, msgHeader->msgLength-sizeOfAddr-sizeof(MSGHEADER), *addr);
                    break;
                }
                case MSG_TYPE_COMMAND:
                {
                    PCMDHEADER pCmd = (PCMDHEADER)payload;
                    uint8_t* ptr = (uint8_t*)payload;
                    ptr += sizeof(CMDHEADER);

                    if( EutranMgrPtr->isProcessingCmd() )
                    {
                        EMLogInfo("processing previous command, discard CMD %d", pCmd->cmdCode);
                        std::vector < WrapperCmdPara > responseParas;
                        responseParas.push_back(WrapperCmdPara(PARA_TYPE_RESULT, static_cast<uint32_t>(PARA_VALUE_RESULT_DOING)));
                        EutranMgrPtr->responseToModule(msgHeader->srcID, msgHeader->msgType,(pCmd->cmdType | 0x10), pCmd->cmdCode, pCmd->cmdIndex, responseParas);
                        break;
                    }

                    EutranMgrPtr->saveMsgCmdCodeAndSrcID(msgHeader, pCmd);
					if( pCmd->cmdCode==CMD_CODE_EUTRAN_SET_SCAN_MODE )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->setScanMode(pParam, len);
                    }
                    else if(pCmd->cmdCode==CMD_CODE_PA_SWITCH_ON )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->handlePaOnResponse(pParam, len);
                    }
                    else if(pCmd->cmdCode==CMD_CODE_PA_SWITCH_OFF )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->handlePaOffResponse(pParam, len);
                    }

					else if( pCmd->cmdCode==CMD_CODE_EUTRAN_CHANG_TELEOPERATOR )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->changTeleOperator(pParam, len);
                    }
					else if( pCmd->cmdCode==CMD_CODE_EUTRAN_LOCATION_ENABLE_SETTING )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->locationEnableSetting(pParam, len);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_CFG_AUTO_SNF )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->cfgAutoSniffer(pParam, len);
                    }
                    else if(pCmd->cmdCode==CMD_CODE_EUTRAN_UEID_FILTER_SWITCH )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->ueIdFilter(pParam, len);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_STD_TX_POWER_DB_GET)
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->processStdTxPowerDbGetMessage(pParam, len);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_STD_TX_POWER_GET)
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->processStdTxPowerGetMessage(pParam, len);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_STD_TX_POWER_CONFIG)
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->processStdTxPowerCfgMessage(pParam, len);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_REDIT_CONFIG )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->processUeRedirectCommand(pParam, len);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_SHOW_BBU_INFO )
                    {
                        EutranMgrPtr->processShowBbuCommand();
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_GPS_STATE_REQ )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->reqGpsState(pParam, len);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_WORK_PERIOD_SETTING )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->WorkPeriodSetting(pParam, len);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_SYNC_STATE_GET )
                    {
                        UNPACK_RESULT uprc;
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        uint32_t paraType;
                        uint32_t paraLength = 0;
                        uint8_t* paraValue = NULL;

                        uint8_t slotId = 0;
                        while((uprc=CM_paraUnpackPara(&pParam, &len, &paraType, &paraLength,&paraValue)) == UNPACK_OK)
                        {
                            if( paraType==PARA_TYPE_EUTRAN_BBU_SLOT_ID )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                slotId = atoi(value);

                                EMLogInfo("slotId=%d", static_cast<uint32_t>(slotId));
                            }
                        }
                        EutranMgrPtr->getSyncState(slotId);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_SYNC_MODE_SETTING )
                    {
                        UNPACK_RESULT uprc;
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        uint32_t paraType;
                        uint32_t paraLength = 0;
                        uint8_t* paraValue = NULL;

                        uint8_t slotId = 0;
                        uint8_t modeCode = 0;
                        while((uprc=CM_paraUnpackPara(&pParam, &len, &paraType, &paraLength,&paraValue)) == UNPACK_OK)
                        {
                            if( paraType==PARA_TYPE_EUTRAN_BBU_SLOT_ID )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                slotId = atoi(value);

                                EMLogInfo("slotId=%d", static_cast<uint32_t>(slotId));
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_SYNC_MODE_CODE )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                modeCode = atoi(value);

                                EMLogInfo("modeCode=%d", static_cast<uint32_t>(modeCode));
                            }
                        }
                        EutranMgrPtr->setSyncMode(slotId, modeCode);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_BBU_UPGRADE )
                    {
                        UNPACK_RESULT uprc;
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        uint32_t paraType;
                        uint32_t paraLength = 0;
                        uint8_t* paraValue = NULL;

                        uint8_t slotId = 0;
                        std::string ipAddr;
                        std::string fileName;
                        std::string md5;
                        std::string userName;
                        std::string passwd;
                        uint8_t downMode = 0;
                        while((uprc=CM_paraUnpackPara(&pParam, &len, &paraType, &paraLength,&paraValue)) == UNPACK_OK)
                        {
                            if( paraType==PARA_TYPE_EUTRAN_BBU_SLOT_ID )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                slotId = atoi(value);

                                EMLogInfo("slotId=%d", static_cast<uint32_t>(slotId));
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_UPGRADE_IP_ADDR )
                            {
                                ipAddr.assign(reinterpret_cast<char*>(paraValue), paraLength);

                                EMLogInfo("ipAddr=%s", ipAddr.c_str());
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_FILE_NAME )
                            {
                                fileName.assign(reinterpret_cast<char*>(paraValue), paraLength);

                                EMLogInfo("fileName=%s", fileName.c_str());
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_FILE_MD5 )
                            {
                                md5.assign(reinterpret_cast<char*>(paraValue), paraLength);
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_UPGRADE_DOWN_MODE )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                downMode = atoi(value);

                                EMLogInfo("downMode is %d", static_cast<uint32_t>(downMode));
                            }
                        }
                        EutranMgrPtr->upgradeBbu(slotId, ipAddr, fileName, md5, userName, passwd, downMode);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_BBU_REBOOT )
                    {
                        UNPACK_RESULT uprc;
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        uint32_t paraType;
                        uint32_t paraLength = 0;
                        uint8_t* paraValue = NULL;

                        uint8_t slotId = 0;
                        std::string desc;
                        while((uprc=CM_paraUnpackPara(&pParam, &len, &paraType, &paraLength,&paraValue)) == UNPACK_OK)
                        {
                            if( paraType==PARA_TYPE_EUTRAN_BBU_SLOT_ID )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                slotId = atoi(value);

                                EMLogInfo("slotId=%d", static_cast<uint32_t>(slotId));
                            }
                            if( paraType==PARA_TYPE_EUTRAN_BBU_REBOOT_DESC_STR )
                            {
                                desc.assign(reinterpret_cast<char*>(paraValue), paraLength);

                                EMLogWarning("desc=%s", desc.c_str());
                            }
                        }
                        EutranMgrPtr->rebootBbu(slotId, desc);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_BBU_STATE_REQ )
                    {
                        UNPACK_RESULT uprc;
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        uint32_t paraType;
                        uint32_t paraLength = 0;
                        uint8_t* paraValue = NULL;

                        uint8_t slotId = 0;
                        while((uprc=CM_paraUnpackPara(&pParam, &len, &paraType, &paraLength,&paraValue)) == UNPACK_OK)
                        {
                            if( paraType==PARA_TYPE_EUTRAN_BBU_SLOT_ID )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                slotId = atoi(value);

                                EMLogInfo("slotId=%d", static_cast<uint32_t>(slotId));
                            }
                        }
                        EutranMgrPtr->requestBbuState(slotId);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_SOFTWARE_VERSION_REQ )
                    {
                        UNPACK_RESULT uprc;
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        uint32_t paraType;
                        uint32_t paraLength = 0;
                        uint8_t* paraValue = NULL;

                        uint8_t slotId = 0;
                        while((uprc=CM_paraUnpackPara(&pParam, &len, &paraType, &paraLength,&paraValue)) == UNPACK_OK)
                        {
                            if( paraType==PARA_TYPE_EUTRAN_BBU_SLOT_ID )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                slotId = atoi(value);

                                EMLogInfo("slotId=%d", static_cast<uint32_t>(slotId));
                            }
                        }
                        EutranMgrPtr->requestSoftwareVersion(slotId);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_TX_POWER_DB_CONFIG )
                    {
                        UNPACK_RESULT uprc;
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        uint32_t paraType;
                        uint32_t paraLength = 0;
                        uint8_t* paraValue = NULL;

                        uint8_t slotId = 0;
                        int8_t txPwerDb = 0;
                        while((uprc=CM_paraUnpackPara(&pParam, &len, &paraType, &paraLength,&paraValue)) == UNPACK_OK)
                        {
                            if( paraType==PARA_TYPE_EUTRAN_BBU_SLOT_ID )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                slotId = atoi(value);

                                EMLogInfo("slotId=%d", static_cast<uint32_t>(slotId));
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_STD_TX_POWER_DB )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                txPwerDb = atoi(value);

                                EMLogInfo("txPwerDB=%d", static_cast<int32_t>(txPwerDb));
                            }
                        }

                        EutranMgrPtr->setTxPowerDb(slotId, txPwerDb);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_SET_BBU_IP )
                    {
                        UNPACK_RESULT uprc;
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        uint32_t paraType;
                        uint32_t paraLength = 0;
                        uint8_t* paraValue = NULL;

                        uint8_t slotId = 0;
                        std::string ipAddr;
                        uint16_t port = 31790;
                        while((uprc=CM_paraUnpackPara(&pParam, &len, &paraType, &paraLength,&paraValue)) == UNPACK_OK)
                        {
                            if( paraType==PARA_TYPE_EUTRAN_BBU_SLOT_ID )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                slotId = atoi(value);

                                EMLogInfo("slotId=%d", static_cast<uint32_t>(slotId));
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_BBU_IP )
                            {
                                ipAddr.assign(reinterpret_cast<char*>(paraValue), paraLength);
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_BBU_PORT )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                port = atoi(value);
                            }
                        }

                        EutranMgrPtr->setBbuIp(slotId,  ipAddr, port);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_INIT_CONFIG )
                    {
                        UNPACK_RESULT uprc;
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        uint32_t paraType;
                        uint32_t paraLength = 0;
                        uint8_t* paraValue = NULL;

                        uint8_t bw = 10;
                        int32_t gpsTmDelay = 0;
                        uint8_t slotId = 0;
                        uint8_t syncMode = 0;
                        uint8_t isSaveFS = 0;
                        uint8_t isMeasureEnable = 0;
                        uint8_t wkBand = 38;
                        uint8_t wkMode = 0;
                        int16_t minRxLev = 63;
                        uint8_t minRxLevOffset = 1;
						std::string snfArfcnPara;
						int32_t operatorType = -1;
                        std::vector < WrapperCmdPara > responseParas;
                        while((uprc=CM_paraUnpackPara(&pParam, &len, &paraType, &paraLength,&paraValue)) == UNPACK_OK)
                        {
                            if( paraType==PARA_TYPE_EUTRAN_BAND_WIDTH)
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                bw = atoi(value);
                                EMLogInfo("bandwidth=%d", static_cast<uint32_t>(bw));
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_SNF_GPS_TM_DELAY)
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                gpsTmDelay = atoi(value);
                                EMLogInfo("gpsTmDelay=%d", gpsTmDelay);
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_BBU_SLOT_ID )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                slotId = atoi(value);
                                EMLogInfo("slotId=%d", static_cast<uint32_t>(slotId));
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_SNF_SYNC_MODE )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                syncMode = atoi(value);
                                EMLogInfo("syncMode=%d", static_cast<uint32_t>(syncMode));
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_IS_SAVE_FS )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                isSaveFS = atoi(value);
                                EMLogInfo("isSaveFS=%d", static_cast<uint32_t>(isSaveFS));
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_BBU_ISMEASUREENABLE  )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                isMeasureEnable= atoi(value);
                                EMLogInfo("isMeasureEnable=%d", static_cast<uint32_t>(isMeasureEnable));
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_BBU_WORKING_BAND )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                wkBand = atoi(value);
                                EMLogInfo("wkBand=%d", static_cast<uint32_t>(wkBand));
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_BBU_WORKING_MODE )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                wkMode = atoi(value);
                                EMLogInfo("wkMode=%d", static_cast<uint32_t>(wkMode));
                            }
							else if( paraType==PARA_TYPE_EUTRAN_BBU_SNF_EARFCN )
                            {
                                snfArfcnPara.assign(reinterpret_cast<char*>(paraValue), paraLength);
                                EMLogInfo("snfArfcnRange=%s", snfArfcnPara.c_str());
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_BBU_OPERATOR )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                operatorType = atoi(value);
                                EMLogInfo("operator=%d", operatorType);
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_MINRXLEV )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                minRxLev = atoi(value);
                                EMLogInfo("minRxLev=%d", minRxLev);
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_MINRXLEVOFFSET )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                minRxLevOffset = atoi(value);
                                EMLogInfo("minRxLevOffset=%d", minRxLevOffset);
                            }
                        }

                        if( bw!=10 && bw!=20 && bw!=5 )
                        {
                            EutranMgrPtr->responseFailure();
                            break;
                        }
                        
                        EutranConfigDataPtr->setBandWidth(slotId, bw)->setGpsDelay(slotId, gpsTmDelay)->setSyncMode(slotId, syncMode)->setIsSaveFS(slotId, isSaveFS)->setWorkingBand(slotId, wkBand)->setIsMeasEnable(slotId, isMeasureEnable)->setWorkingMode(slotId, wkMode)->setMinRxLev(slotId, minRxLev)->setMinRxLevOffset(slotId, minRxLevOffset);    
                        EutranMgrPtr->responseSuccess();
                        EutranMgrPtr->setBbuConfiged(slotId);
						EutranMgrPtr->setBbuSnfArfcn(slotId,  snfArfcnPara);
                        if (slotId > 5 && operatorType != -1)
                        {
                            EutranMgrPtr->changBbuOperator(operatorType, slotId);
                        }
                    }
                    else if( pCmd->cmdCode ==  CMD_CODE_REDIRECT_INIT_CONFIG)
                    {
                        EutranMgrPtr->responseSuccess();
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t paraType = pParam->paraType;
                        uint32_t paraLength  = pParam->paraLength;


                        UE_REDIRECT *redirect_info = get_redirect_info();

                       
                        memcpy(redirect_info, pParam->paraValue, paraLength);
						
#if 0
                        int i = 0;
                        for(; i < 7; ++i)
                        {
                            EMLogNotice("enable %d\n", redirect_info[i].enable);
                            EMLogNotice("slotId %d\n", redirect_info[i].slotId);
                            EMLogNotice("ranType %d\n",redirect_info[i].ranType);
                            EMLogNotice("band %d\n", redirect_info[i].band);
                            EMLogNotice("freqList[0] %d\n", redirect_info[i].freqList[0]);
                            EMLogNotice("freqList[1] %d\n", redirect_info[i].freqList[1]);
                            EMLogNotice("freqList[2] %d\n", redirect_info[i].freqList[2]);
                            EMLogNotice("freqList[3] %d\n", redirect_info[i].freqList[3]);
                            EMLogNotice("freqList[4] %d\n", redirect_info[i].freqList[4]);
                            EMLogNotice("imsiList[0] %s\n", redirect_info[i].imsiList[0]);
                            EMLogNotice("imsiList[1] %s\n", redirect_info[i].imsiList[1]);
                            EMLogNotice("imsiList[2] %s\n", redirect_info[i].imsiList[2]);
                            EMLogNotice("imsiList[3] %s\n", redirect_info[i].imsiList[3]);
                            EMLogNotice("imsiList[4] %s\n", redirect_info[i].imsiList[4]);
                        }

                       

                        EMLogNotice("CMD_CODE_REDIRECT_INIT_CONFIG\n");
#endif

                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_LOCATION_MODE_SETTING )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->locationModeSetting(pParam, len);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_BLACKLIST_SETTING )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->BlackListModeSetting(pParam, len);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_START_SNIFFER )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->startSnifferManual(pParam, len);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_GET_SNIFFER_STATUS )
                    {
                        int8_t slotId = -1;
                        if( pCmd->cmdLength!=0 )
                        {
                            uint8_t* ptr = (uint8_t*)payload;
                            ptr += sizeof(CMDHEADER);
                            PARAM *tlv = (PARAM *)ptr;
                            if( tlv->paraType==PARA_TYPE_EUTRAN_BBU_SLOT_ID )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, tlv->paraValue, tlv->paraLength);
                                slotId = atoi(value); 
                            }
                        }
                        EutranMgrPtr->requestSnifferStatus(slotId);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_STOP_SNIFFER )
                    {
                        EutranMgrPtr->stopSniffer();
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_RESET_SNIFFER )
                    {
                        EutranMgrPtr->resetSniffer();
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_CONFIG_RSSI )
                    {                        
                        uint8_t slotId = 0;
                        uint16_t rssi = 0;                                                
                        UNPACK_RESULT uprc;
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        uint32_t paraType;
                        uint32_t paraLength;
                        uint8_t* paraValue = NULL;
                        while((uprc=CM_paraUnpackPara(&pParam, &len, &paraType, &paraLength,&paraValue)) == UNPACK_OK)
                        {
                            if( paraType==PARA_TYPE_EUTRAN_BBU_SLOT_ID )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                slotId = atoi(value);
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_SNIFFER_RSSI )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                rssi = atoi(value);
                            }
                        }
                        EutranConfigDataPtr->setRssiThreshold(slotId, rssi);
                        EutranMgrPtr->responseSuccess();
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_GET_SNIFFER_RESULT )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->reportSnifferResult(pParam, len);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_GET_RSSI )
                    {
                        uint16_t rssi = EutranConfigDataPtr->getRssiThreshold(0);

                        std::vector < WrapperCmdPara > responseParas;
                        responseParas.push_back(WrapperCmdPara(PARA_TYPE_EUTRAN_SNIFFER_RSSI, rssi));          
                        EutranMgrPtr->responseToSrcModule(CMD_GET_RESPONSE, responseParas);
                    }
                    else if( pCmd->cmdCode == CMD_CODE_EUTRAN_LTE_SCAN )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->scanLte(pParam, len);
                    }
                    else if ( pCmd->cmdCode == CMD_CODE_EUTRAN_INTERFERENCE_ENABLE )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->enableInterference(pParam, len);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_CONFIG_CELL )
                    {
                        std::vector < WrapperCmdPara > responseParas;
                        
                        uint8_t slotId = 0;
                        uint16_t dlEarfcn = 0;
                        uint16_t ulEarfcn = 0;
                        uint16_t pci = 0;
                        std::vector<uint32_t> plmnList;
                        uint16_t tac = 0;
                        std::vector<uint16_t> intraFreqPci;
                        std::vector<uint16_t> interFreq;
                        bool isMeasEnable = false;
                        
                        UNPACK_RESULT uprc;
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        uint32_t paraType;
                        uint32_t paraLength;
                        uint8_t* paraValue = NULL;
                        bool isParaValid = true;
						uint8_t snifferType = PARA_VALUE_EUTRAN_OAM_SNIFFER;
                        while((uprc=CM_paraUnpackPara(&pParam, &len, &paraType, &paraLength,&paraValue)) == UNPACK_OK)
                        {
                            if( paraType==PARA_TYPE_EUTRAN_BBU_SLOT_ID )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                slotId = atoi(value);
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_DL_ARFCN )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                dlEarfcn = atoi(value);
                                EMLogInfo("cell config dlearfcn: %s", value);
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_UL_ARFCN )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                ulEarfcn = atoi(value);
                                EMLogInfo("cell config ulearfcn: %s", value);
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_PCI)
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                pci = atoi(value);
                                EMLogInfo("cell config pci: %s", value);
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_PLMN_ID )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);

                                std::string delim(",");
                                std::string s(value);
                                std::vector<std::string> plmnStr;
                                split(s, delim, &plmnStr);

                                for(size_t i=0; i<plmnStr.size(); i++)
                                {         
                                    if (plmnStr[i].find("460") == std::string::npos)
                                        continue;                           
									plmnStr[i] += 'F';
                                    EMLogInfo("cell config plmnid: %s", plmnStr[i].c_str());
                                    plmnList.push_back(strtol(plmnStr[i].c_str(), NULL, 16));                           
                                }
                                if (plmnList.size() == 0)
                                    isParaValid = false;
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_TAC )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                tac = atoi(value);
                                EMLogInfo("cell config tac: %d", tac);
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_SNIFFER_TYPE )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                snifferType = atoi(value);
                                EMLogInfo("cell config snifferType: %d", snifferType);
                            }		
                            else if(paraType==PARA_TYPE_EUTRAN_SNIFFER_PCI_LIST )
                            {
                                if( paraLength%sizeof(uint16_t)!=0 )
                                {
                                    EMLogError("Invalid pcilist parameter length[%d]", paraLength);
                                    isParaValid = false;
                                    break;
                                }
                                uint16_t* p16 = reinterpret_cast<uint16_t*>(paraValue);
                                for(uint32_t i=0; i<paraLength/2; i++)
                                {
                                    uint16_t pci = p16[i];
                                    intraFreqPci.push_back(pci);
                                    EMLogInfo("cell config pciList: %d", pci);
                                }
                            }
                            else if(paraType==PARA_TYPE_EUTRAN_ARFCN_LIST)
                            {
                                if( paraLength%sizeof(uint16_t)!=0 )
                                {
                                    EMLogError("Invalid interFreq parameter length[%d]", paraLength);
                                    isParaValid = false;
                                    break;
                                }
                                uint16_t* p16 = reinterpret_cast<uint16_t*>(paraValue);
                                for(uint32_t i=0; i<paraLength/2; i++)
                                {
                                    uint16_t arfcn = p16[i];
                                    interFreq.push_back(arfcn);
                                }
                            }
                            else if(paraType==PARA_TYPE_EUTRAN_IS_MEASURE_ENABLE)
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);

                                uint8_t v8 = atoi(value);
                                if( v8!=0 )
                                {
                                    isMeasEnable = true;
                                }
                                EMLogInfo("cell config isMeasEnable: %d", isMeasEnable);
                            }
                            else
                            {
                                EMLogError("Unknown paraType[%d]", paraType);
                                isParaValid = false;
                            }
                            if( !isParaValid )
                            {
                                EMLogError("paraType[0x%x] is invalid", paraType);
                                
                                responseParas.push_back(WrapperCmdPara(PARA_TYPE_RESULT, static_cast<uint32_t>(PARA_VALUE_RESULT_FAILED)));
                                responseParas.push_back(WrapperCmdPara(PARA_TYPE_ERROR_CODE, static_cast<uint32_t>(PARA_VALUE_ERROR_INVALID_PARAVALUE)));
                                EutranMgrPtr->responseToSrcModule(CMD_SET_RESPONSE, responseParas);

                                break;
                            }                            
                        }

                        if( isParaValid )
                        {
                            if( ulEarfcn==0 )
                            {
                                EMLogInfo("Not spec UL earfcn, set to DL earfcn[%d]", dlEarfcn);
                                ulEarfcn = dlEarfcn;
                            }
                            EutranMgrPtr->configCell(slotId, dlEarfcn, ulEarfcn, pci, tac, plmnList, intraFreqPci, interFreq, NULL, isMeasEnable, snifferType);
                        }
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_UPDATE_CELL )
                    {
                        std::vector < WrapperCmdPara > responseParas;
                        
                        UNPACK_RESULT uprc;
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        uint32_t paraType;
                        uint32_t paraLength;
                        uint8_t* paraValue = NULL;
                        bool isParaValid = true;

                        uint8_t slotId = 0;
                        std::vector<uint32_t> plmnList;
                        uint16_t tac = 0;
                        uint16_t* tacPtr = NULL;
                        uint16_t earfcn = 0;
                        uint16_t* earfcnPtr = NULL;
                        bool isMeasEnable = false;
                        bool* measEnablePtr = NULL;
                        while((uprc=CM_paraUnpackPara(&pParam, &len, &paraType, &paraLength,&paraValue)) == UNPACK_OK)
                        {
                            if( paraType==PARA_TYPE_EUTRAN_PLMN_ID )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);

                                std::string delim(",");
                                std::string s(value);
                                std::vector<std::string> plmnStr;
                                split(s, delim, &plmnStr);

                                for(size_t i=0; i<plmnStr.size(); i++)
                                {
                                    if (plmnStr[i].find("460") == std::string::npos)
                                        continue;
									plmnStr[i] += 'F';
                                    EMLogInfo("cell update plmnid: %s\n", plmnStr[i].c_str());
                                    plmnList.push_back(strtol(plmnStr[i].c_str(), NULL, 16));   
                                }
                                if (plmnList.size() == 0)
                                    isParaValid = false;
                            }
                            else if( paraType==PARA_TYPE_EUTRAN_TAC )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                tac = atoi(value);
                                tacPtr= &tac;
                            }
                            else if(paraType==PARA_TYPE_EUTRAN_DL_ARFCN )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                earfcn = atoi(value);
                                earfcnPtr= &earfcn;
                            }
                            else if(paraType==PARA_TYPE_EUTRAN_BBU_SLOT_ID )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);
                                slotId = atoi(value);
                            }
                            else if(paraType==PARA_TYPE_EUTRAN_IS_MEASURE_ENABLE )
                            {
                                char value[128];
                                memset(value, '\0', sizeof(value));
                                memcpy(value, paraValue, paraLength);

                                uint8_t v8 = atoi(value);
                                if( v8!=0 )
                                {
                                    isMeasEnable = true;
                                }
                                EMLogInfo("cell update isMeasEnable: %d\n", isMeasEnable);
                                measEnablePtr = &isMeasEnable;
                            }
                            else
                            {
                                EMLogError("Unknown paraType[%d]", paraType);
                                isParaValid = false;
                            }
                            if( !isParaValid )
                            {
                                EMLogError("paraType[0x%x] is invalid", paraType);
                                
                                responseParas.push_back(WrapperCmdPara(PARA_TYPE_RESULT, static_cast<uint32_t>(PARA_VALUE_RESULT_FAILED)));
                                responseParas.push_back(WrapperCmdPara(PARA_TYPE_ERROR_CODE, static_cast<uint32_t>(PARA_VALUE_ERROR_INVALID_PARAVALUE)));
                                EutranMgrPtr->responseToSrcModule(CMD_SET_RESPONSE, responseParas);

                                break;
                            }
                        }

                        EutranMgrPtr->UpdateCell(slotId, &plmnList, tacPtr, earfcnPtr, measEnablePtr, false);
                    }
                    else if( pCmd->cmdCode==CMD_CODE_PM_RESTART )
                    {
                        EutranMgrPtr->processPmRestart();
                    }
                    else if( pCmd->cmdCode==CMD_CODE_EUTRAN_RT_PARA_CFG )
                    {
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        EutranMgrPtr->cfgRtParameter(pParam, len);
                    }
#ifdef MODULE_TEST
                    else if( pCmd->cmdCode==CMD_CODE_SYSMONITOR_TEST_MODULE )
                    {                                           
                        UNPACK_RESULT uprc;
                        PPARAM pParam =(PPARAM)(pCmd+1);
                        uint32_t len = pCmd->cmdLength;
                        uint32_t paraType;
                        uint32_t paraLength;
                        uint8_t* paraValue = NULL;
                        while((uprc=CM_paraUnpackPara(&pParam, &len, &paraType, &paraLength,&paraValue)) == UNPACK_OK)
                        {
                            if( paraType==PARA_TYPE_SYSMONITOR_TEST )
                            {
                                if ( *(U32 *)paraValue == PARA_VALUE_SYSMONITOR_MAIN  )
                                {
                                     EMLogError("---------- test module eutran task main ---------------- %04x\n",
                                                pCmd->cmdCode);
                                     while(1)
                                     {
                                     
                                     }
                                }
                                else if ( *(U32 *)paraValue == PARA_VALUE_SYSMONITOR_THREAD1 )
                                {
                                    testUdp = true;
                                }
                            }
                           
                        }

                    }
#endif
                    else if( pCmd->cmdCode == CMD_CODE_SYS_EXIT)
                    {
                        dbgprintf(DEBUG_EMERG, stderr, "modId[0x%x] system is exiting, closing EUTRAN socket. \n", MODULE_EUTRAN_SNIFFER);

                        struct timeval tv;

                        tv.tv_sec = 1;
                        tv.tv_usec = 0;
                        int32_t maxFd = EutranTcpReceiverPtr->getMaxFd() + 1;
                        fd_set readfds = EutranTcpReceiverPtr->getReadFdSet();
                        fd_set writefds = EutranTcpReceiverPtr->getWriteFdSet();
                        fd_set exceptionFds = EutranTcpReceiverPtr->getExceptionFdSet();
                        int32_t selectRc = select(maxFd, &readfds, &writefds,  &exceptionFds, &tv);
        
                        if( selectRc<=0 )
                        {
                            break;
                        }

                        EutranTcpReceiverPtr->handleReadEvents(&readfds);
                        EutranTcpReceiverPtr->handleWriteEvents(&writefds);
                        EutranTcpReceiverPtr->handleExcepEvents(&exceptionFds);                        
                    }
                    else
                    {
                        EMLogError("Invalid command code[%d]", pCmd->cmdCode);
                    }
                    break;
                }
                default:
                {
                    EMLogError("eutranCmdReceiver: invalid msg type[%d]", msgHeader->msgType);
                }
            }
        }
        else
        {
            struct timeval tv;

            tv.tv_sec = 1;
            tv.tv_usec = 0;
            int32_t maxFd = EutranTcpReceiverPtr->getMaxFd() + 1;
            fd_set readfds = EutranTcpReceiverPtr->getReadFdSet();
            fd_set writefds = EutranTcpReceiverPtr->getWriteFdSet();
            fd_set exceptionFds = EutranTcpReceiverPtr->getExceptionFdSet();
            int32_t selectRc = select(maxFd, &readfds, &writefds,  &exceptionFds, &tv);
        
            if( selectRc<=0 )
            {
                continue;
            }

            EutranTcpReceiverPtr->handleReadEvents(&readfds);
            EutranTcpReceiverPtr->handleWriteEvents(&writefds);
            EutranTcpReceiverPtr->handleExcepEvents(&exceptionFds);
        }
    }

    pthread_exit((void*)0);
}

bool createUdpTask()
{
    pthread_t       thread;
    if( pthread_create(&thread, NULL, eutranUdpRecv, 0) )
    {
        EMLogError("pthread_create createUdpTask thread failed: %s", strerror(errno));
        return false;
    }
    return true;
}

bool createEutranMainTask()
{    
    pthread_t       thread;
    if( pthread_create(&thread, NULL, eutranMainTask, 0) )
    {
        EMLogError("pthread_create eutranMainTask thread failed: %s", strerror(errno));
        return false;
    }
    return true;
}

RESULT EUTRAN_init(void)
{ 
    if( !EutranMgrPtr->initialize() )
    {
        dbgprintf(DEBUG_ERROR, stderr, "%s: Fail to initialize EutranMgr\n", __func__);
        return RC_FAIL;
    }

    if( RC_OK != MOD_register(EutranMgrPtr->getModRegInfo()) )
    {
        dbgprintf(DEBUG_ERROR, stderr, "%s: Can't register module with eutranMgr\n", __func__);
        return RC_FAIL;
    }

#ifndef USE_TCP_CONN
    if( !createUdpTask() )
    {
        EMLogError("Fail to createUdpTask", __func__);
        return RC_FAIL;
    }
#endif
    
    if( !createEutranMainTask() )
    {
        EMLogError("Fail to createEutranMainTask");
        return RC_FAIL;
    }

    EMLogInfo("EUTRAN_init is successful");
    return RC_OK;
}

